package model.filemanager;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

public class MediaFile {

	private final int DEV_MODE = 1; 
	
	private Entity entity;
	
	public MediaFile(Entity entity) throws EntityNotFoundException
	{
		if(entity != null) {
			this.entity = entity;
		} else {
			throw new EntityNotFoundException(null);
		}
	}
	
	public String getName()
	{
		return (String)entity.getProperty("name");
	}
	
	public String getKey()
	{
		return (String)entity.getProperty("blobkey");
	}
	
	public String getType()
	{
		return (String)entity.getProperty("type");
	}
	
	public Object getProperty(String property)
	{
		return entity.getProperty(property);
	}
	
	public void setProperty(String property, Object value)
	{
		entity.setProperty(property, value);
	}
	
	public Entity getEntity()
	{
		return entity;
	}
	
	public long getFileSize()
	{
		return (Long) entity.getProperty("size");
	}
	
	public String getImageUrl(int size, boolean crop)
	{
		switch(DEV_MODE)
		{
		case 1:
			if(entity.getProperty("type").equals("image") ){
				ImagesService imageService = ImagesServiceFactory.getImagesService();
				String result = imageService.getServingUrl(new BlobKey((String)entity.getProperty("blobkey")), size, crop);
				return result;
			}
		break;
		case 0:
			if(entity.getProperty("type").equals("image") ){
				return "/downloadfile?download=no&key=" + entity.getProperty("blobkey");
			}
		break;
		}
		if(entity.getProperty("type").equals("audio") ){
			return "/images/music.png";
		}
		if(entity.getProperty("type").equals("video") ){
			return "/images/video.png";
		}
		return "/images/noImageAvailable.jpg";
	}
	
}
